#include "SplashManager.h"


#include "../../Support/GameSupport.h"
#include "../../Framework/SceneManager.h"
#include "../Menu/MenuManager.h"
#include "../../Data/GameData.h"
#include "../Play/PlayManager.h"
#include "../Level/LevelManager.h"
#include "../../Player/PlayerInfo.h"
#include "../../Framework/SoundManager.h"

using namespace cocostudio;

Scene* SplashManager::createScene()
{
	auto scene = Scene::create();
	auto layer = SplashManager::create();
	scene->addChild(layer);
	return scene;
}

bool SplashManager::init()
{
	if (!Layer::init())
	{
		return false;
	}

	this->setKeypadEnabled(true);

	m_screenSize = Director::getInstance()->getVisibleSize();
    SoundManager::getInstance()->playMusic(true);
    
	initalize();
	return true;
}

SplashManager::~SplashManager()
{
	SpriteFrameCache::getInstance()->removeUnusedSpriteFrames();
	SpriteFrameCache::getInstance()->removeSpriteFrames();
	TextureCache::getInstance()->removeAllTextures();
}

void SplashManager::initalize()
{
	m_onCautionLayer = false;
	m_random = random(5, 8);
	m_valueEachFrame = Director::getInstance()->getAnimationInterval() / m_random * 300;

	initBackground();
	initLayer();
	initComponent();

	loadResouce();

	this->scheduleUpdate();
}

void SplashManager::initBackground()
{
	Sprite* background = Sprite::create("bg_intro.png");
	background->setPosition(m_screenSize.width / 2, m_screenSize.height / 2);
	float scaleX = m_screenSize.width / background->getContentSize().width;
	float scaleY = m_screenSize.height / background->getContentSize().height;
	background->setScale(scaleX, scaleY);
	this->addChild(background);
}

void SplashManager::initLayer()
{
	auto node = CSLoader::getInstance()->createNode("SplashScene.csb");

	float scale = m_screenSize.height / node->getContentSize().height;
	node->setScale(scale);
	node->setAnchorPoint(Vec2(0.5f, 0.5f));
	node->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
	node->setTag(ESplashTag::kSplashTag_SceneNode);

	Vector<Node*> list = node->getChildren();
	layerCaution = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Caution));
	layerLoading = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Loading));
	layerDesigner = list.at(0)->getChildByName(GameSupport::getTextSplashLayer(ESplashLayer::kSplashLayer_Designer));

	this->addChild(node);
}

void SplashManager::initComponent()
{
	//Caution - button OK
	ui::Button* btnOK = static_cast<ui::Button*>(layerCaution
		->getChildByTag(ESplashCocoTag::kSplashCoco_Board)
		->getChildByTag(ESplashCocoTag::kSplashCoco_OK));
	btnOK->addClickEventListener(CC_CALLBACK_1(SplashManager::tapOK, this));
	btnOK->setPressedActionEnabled(true);

	// btnOK->setScale9Enabled(true);
	//Loading - loading bar
	m_loadingBar = static_cast<LoadingBar*>(layerLoading->getChildByTag(ESplashCocoTag::kSplashCoco_ProcessbarBG)->getChildByTag(ESplashCocoTag::kSplashCoco_ProcessBarFG));
	m_loadingBar->setPercent(0);
}

void SplashManager::tapOK(Ref* pSender)
{

    SoundManager::getInstance()->playSound(ESound::kSound_Click);
	SceneManager::getInstance()->changeScene(this, EScene::kScene_Menu);
}

void SplashManager::runLayerCaution()
{
	Point p = layerCaution->getPosition();
	layerCaution->setPosition(p.x, m_screenSize.height * 3 / 2);
	layerCaution->setVisible(true);
	layerCaution->runAction(MoveTo::create(0.5f, p));

	layerDesigner->runAction(MoveTo::create(1.5f, Vec2(layerDesigner->getPosition().x, -m_screenSize.height)));

	this->runAction(Sequence::create(DelayTime::create(1.5f),
		CallFuncN::create(CC_CALLBACK_0(SplashManager::setOnCautionLayer, this)), NULL));
}

void SplashManager::setOnCautionLayer()
{
	m_onCautionLayer = true;
}

void SplashManager::runLayerDesign()
{
	layerDesigner->setVisible(true);
	layerLoading->setVisible(false);
}

void SplashManager::update(float dt)
{
	if (m_loadingBar->getPercent() >= 100)
	{
		auto gotoDesigner = CallFuncN::create(CC_CALLBACK_0(SplashManager::runLayerDesign, this));
		auto gotoCaution = CallFuncN::create(CC_CALLBACK_0(SplashManager::runLayerCaution, this));

		this->runAction(Sequence::create(gotoDesigner, DelayTime::create(1.5f), gotoCaution, NULL));
		this->unscheduleUpdate();
	}
	else
	{
		m_loadingBar->setPercent(m_loadingBar->getPercent() + m_valueEachFrame);
	}
}

void SplashManager::loadResouce()
{
	GameData::getInstance()->readConfig();

	//MenuManager::createScene();
	//LevelManager::createScene();
	//PlayManager::createScene();
	GhostObject::ReadPositionComponent();
}

void SplashManager::onKeyReleased(EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
	// Back button pressed
	if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE)
	{
		if (m_onCautionLayer)
		{
			Director::getInstance()->end();
			exit(0);
		}
	}
}

void SplashManager::onExit()
{
	Node* node = this->getChildByTag(ESplashTag::kSplashTag_SceneNode);
	node->removeFromParent();
	node = nullptr;

	Layer::onExit();
}